﻿@{
	PageData["Title"] = "Change Password";
	WebSecurity.RequireAuthenticatedUser();

	var db = Database.Open("wishlistdb");
	var currentUser = db.QuerySingle("SELECT Username FROM WishlistUser WHERE Id = @0", WebSecurity.CurrentUserId);
}

<h1>Account Management</h1>
@if (IsPost) {
	var oldPassword = Request["oldPassword"];
	var newPassword = Request["newPassword"];
	var newPasswordConfirm = Request["newPasswordConfirm"];
    var errorMessage = "";
    //If user has entered matching passwords, update their password
	if (newPassword == newPasswordConfirm && !newPassword.IsEmpty() && !oldPassword.IsEmpty()) {
		if (WebSecurity.ChangePassword (WebSecurity.CurrentUserName, oldPassword, newPassword)) {
			<div class="success">Password Updated!</div>
		} else {
			errorMessage = "<li>There was a problem updating your pasword.  Try again.</li>";
		}
	} else {
		errorMessage = "<li>Passwords don't match or are empty!  Try again.</li>";
	}
    if (!errorMessage.IsEmpty()) {
        @RenderPage("~/Shared/Errors/_FormInputError.cshtml", errorMessage);
    }
}
<div>
    Hello @currentUser.Username, Please update your password using the form below:
    <form id="validateForm" method="post" action="">
        <fieldset>
	        <legend>Password Update</legend>
	        @RenderPage("~/Shared/_InputFormField.cshtml",
                new { labelName = "oldPassword", labelDisplay = "Current Password:", inputType = "password", required = true})
	        @RenderPage("~/Shared/_InputFormField.cshtml",
                new { labelName = "newPassword", labelDisplay = "New Password:", inputType = "password", required = true})
	        @RenderPage("~/Shared/_InputFormField.cshtml",
                new { labelName = "newPasswordConfirm", labelDisplay = "Confirm New Password:", inputType = "password", required = true, 
                    validationAttributes = "equalTo=\"#newPassword\""})
	        <div class="submit-group">
		        <input type="submit" value="Update" onclick="javascript:$('#validateForm').validate()" />
	        </div>
        </fieldset>
    </form>
</div>